# -*- coding: utf-8 -*-
# @Time    : 2020/12/7 20:08
# @Author  : Guo_Zhanyu
# @File    : Gomoku.py
# @Comment : Store all Classes and Functions
# @toImport: from Gomoku import *

"""Imports"""
import pygame

# 棋盘窗口绘制
"""Globals"""
SIZE = 30  # 棋盘每个点的距离
LINES = 19  # 棋盘的行/列数
OUTER_WIDTH = 20  # 棋盘外宽度
INSIDE_WIDTH = 4  # 边框与棋盘最外的小间隔
BORDER_WIDTH = 4  # 边框宽度
BORDER_LENGTH = SIZE*(LINES - 1) + INSIDE_WIDTH*2 + BORDER_WIDTH  # 边框线长度
START_X = START_Y = OUTER_WIDTH + int(BORDER_WIDTH/2) + INSIDE_WIDTH  # 边框左上角坐标
SCREEN_WIDTH = SCREEN_HEIGHT = SIZE*(LINES - 1) + OUTER_WIDTH*2 + BORDER_WIDTH + INSIDE_WIDTH*2  # 屏幕高度
# SCREEN_WIDTH = SCREEN_HEIGHT + 200  # 游戏屏幕的宽

# 棋子绘制
STONE_R = SIZE//2 - 3  # 棋子半径
# STONE_R2 = SIZE//2 + 3

# 颜色
# BOARD_COLOR = (0xE3, 0x92, 0x65)  # 背景颜色
BOARD_COLOR = (0xFA, 0xBE, 0x6E)  # 背景颜色
BLACK_COLOR = (0, 0, 0)  # 黑色
WHITE_COLOR = (255, 255, 255)  # 白色
RED_COLOR = (200, 30, 30)  # 红色
# BLUE_COLOR = (30, 30, 200)  # 蓝色

# 偏移量，用以遍历棋盘
offset = [(1, 0), (0, 1), (1, 1), (1, -1)]


# black or white，value = 1为黑，2为白
class Chessman:
    def __init__(self, name, value, color):
        self.name = name  # black / white
        self.value = value  # 1 / 2
        self.color = color  # (219, 219, 219) / (45, 45, 45)
        pass
    pass


BLACK_CHESSMAN = Chessman('black', 1, (45, 45, 45))
WHITE_CHESSMAN = Chessman('white', 2, (219, 219, 219))


# 游戏坐标系，左上角为(0，0)，最大为(LINES - 1, LINES - 1)
# 竖直向下为Y轴方向，水平向右为X轴方向
class Point:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y
        pass
    pass


# 棋盘
class Board:
    def __init__(self, lines):
        self._lines = lines
        self._board = [[0] * lines for _ in range(lines)]  # 矩阵[LINESxLINES]
        pass

    def getBoard(self):
        return self._board

    def setBoard(self, new_board):
        self._board = new_board
        pass

    def canDrop(self, point):
        return self._board[point.y][point.x] == 0

    def drop(self, chessman, point):
        self._board[point.y][point.x] = chessman.value

        # 判断胜利
        if self.win(point):
            print(f'{chessman.name} win, (Press SPACE to continue...)')
            return chessman
        pass

    def win(self, point):
        cur_value = self._board[point.y][point.x]
        for item in offset:
            if self.getCountOnDirection(point, cur_value, item[0], item[1]) >= 5:
                return True
            pass
        pass

    def getCountOnDirection(self, point, value, x_offset, y_offset):
        count = 1
        for step in range(1, 5):
            x = point.x + step*x_offset
            y = point.y + step*y_offset
            if 0 <= x < self._lines and 0 <= y < self._lines and self._board[y][x] == value:
                count += 1
            else:
                break
                pass
            pass

        for step in range(1, 5):
            x = point.x - step*x_offset
            y = point.y - step*y_offset
            if 0 <= x < self._lines and 0 <= y < self._lines and self._board[y][x] == value:
                count += 1
            else:
                break
                pass
            pass

        return count

    pass


def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
    imgText = font.render(text, True, fcolor)
    screen.blit(imgText, (x, y))
    pass


def drawBoard(screen):
    # 填充颜色
    screen.fill(BOARD_COLOR)
    # 画外边框
    pygame.draw.rect(screen, BLACK_COLOR, (OUTER_WIDTH, OUTER_WIDTH, BORDER_LENGTH, BORDER_LENGTH), BORDER_WIDTH)
    # 画网格线
    # 水平
    for i in range(LINES):
        pygame.draw.line(screen, BLACK_COLOR,
                         (START_X, START_Y + SIZE*i),
                         (START_X + SIZE*(LINES - 1), START_Y + SIZE*i),
                         1)
        pass
    # 垂直
    for j in range(LINES):
        pygame.draw.line(screen, BLACK_COLOR,
                         (START_X + SIZE*j, START_Y),
                         (START_X + SIZE*j, START_Y + SIZE*(LINES - 1)),
                         1)
        pass
    # 画点
    for i in (3, 9, 15):
        for j in (3, 9, 15):
            if i == j == 9:
                r = 5
            else:
                r = 3
                pass
            pygame.draw.circle(screen, BLACK_COLOR, (START_X + SIZE*i, START_Y + SIZE*j), r, 0)
            pass
        pass
    pass


def drawStones(board, screen):
    for i, row in enumerate(board.getBoard()):
        for j, cell in enumerate(row):
            if cell == BLACK_CHESSMAN.value:
                drawStone(screen, Point(j, i), BLACK_CHESSMAN.color)
            elif cell == WHITE_CHESSMAN.value:
                drawStone(screen, Point(j, i), WHITE_CHESSMAN.color)
                pass
            pass
        pass
    pass


def drawStone(screen, point, stone_color):
    pygame.draw.circle(screen, stone_color, (START_X + SIZE*point.x, START_Y + SIZE*point.y), STONE_R, 0)
    pass


def getClickPoint(click_pos):
    pos_x = click_pos[0] - START_X
    pos_y = click_pos[1] - START_Y
    if pos_x < -INSIDE_WIDTH or pos_y < -INSIDE_WIDTH:
        return None
    x = pos_x//SIZE
    y = pos_y//SIZE
    if pos_x % SIZE > SIZE/2:
        x += 1
        pass
    if pos_y % SIZE > SIZE/2:
        y += 1
        pass
    if x >= LINES or y >= LINES:
        return None

    return Point(x, y)
    pass


def getNextRunner(cur_runner):
    if cur_runner == BLACK_CHESSMAN:
        return WHITE_CHESSMAN
    else:
        return BLACK_CHESSMAN
    pass
